#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <deque>

using namespace std;

deque<int> c[25], temp;
int a, b, n;
string s, s1, s2;

int row(int v)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < c[i].size(); j++)
			if (c[i][j] == v) return i;
}

int main()
{
	scanf("%d\n", &n);
	for (int i = 0; i < n; i++)
	{
		c[i].clear();
		c[i].push_back(i);
	}

	while (true)
	{
		getline(cin, s);
		if (s == "quit") break;

		istringstream ss(s);

		temp.clear();

		ss >> s1 >> a >> s2 >> b;
		int ra = row(a), rb = row(b), v;
		if (a == b || ra == rb) continue;

		if (s1 == "move")
			do{
				v = c[ra].back();
				c[ra].pop_back();
				if (v == a)
				{
					temp.push_front(v);
					break;
				}
				c[v].push_front(v);
			} while (v != a);
		else
			do{
				v = c[ra].back();
				c[ra].pop_back();
				temp.push_front(v);
			} while (v != a);

		if (s2 == "onto")
			while (true)
			{
				v = c[rb].back();
				if (v == b) break;
				c[rb].pop_back();
				c[v].push_front(v);
			}

		while (!temp.empty())
		{
			c[rb].push_back(temp.front());
			temp.pop_front();
		}

	}

	for (int i = 0; i < n; i++)
	{
		cout << i << ":";
		for (int j = 0; j < c[i].size(); j++) cout << " " << c[i][j] ;
		cout << endl;
	}
	return 0;
}
